<template>
  <div id="app">
    <el-container class="home-container">
      <!-- 头部区域 -->
      <el-header>
        <div>
          <img src="https://tse1-mm.cn.bing.net/th/id/OIP-C.vR3c8gJDtTZuFFJLa3nHcwHaHC?w=173&h=180&c=7&r=0&o=5&dpr=1.3&pid=1.7">
          <span>电商管理平台</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <!-- 页面主体 -->
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <div class="toggle-button" @click="toggleCollapse">|||</div>
<!--          侧边栏效果-->
          <el-menu
              active-color="#409eff"
              background-color="#333744"
              text-color="#fff"
              :unique-opened="true"
              :collapse="isCollapse"
              :collapse-transition="false"
              :router="true"
              :default-active="activePath"
          >
<!--            一级菜单-->
            <el-sub-menu :index="item.id + ''" v-for="item in menulist" :key="item.id">
<!--              一级菜单的模板区域-->
              <template #title>
<!--                图标-->
                <i :class="inconObej[item.id]"></i>
<!--                文本-->
                <span>{{item.authName}}</span>
              </template>
<!--              二级菜单-->
              <el-menu-item :index="'/'+subitem.path" v-for="subitem in item.children" :key="subitem.id" @click="saveNavState('/'+subitem.path)"> <template #title>
                <!--                图标-->
                <el-icon><Menu/></el-icon>
                <!--                文本-->
                <span>{{ subitem.authName
                  }}</span>
              </template></el-menu-item>


            </el-sub-menu>

          </el-menu>
          </el-aside>
        <!-- 内容主题 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
export default {
  created() {
    this.getMenuList()
    this.activePath =  window.sessionStorage.getItem('activePath')
  },
  data() {
    return {
      menulist: [],
      // icon图标
      inconObej:{
        '125':'iconfont icon-yonghuguanli',
        '103':'iconfont icon-lifangtilitiduomiantifangkuai2',
        '101':'iconfont icon-shangpinguanli-jianying',
        '102':'iconfont icon-shangpinguanli-jianying',
        '145':'iconfont icon-shujutongji',
      },
      //是否切换
      isCollapse:false,
    //   被激活的链接
      activePath:''
    }
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // 获取所有的菜单
    async getMenuList() {
      const {data: res} = await this.$http.get('/menus')
      if(res.meta.status !== 200) return this.$message.error(res.msg)
      this.menulist = res.data
      // console.log(res)
    },
    // 点击按钮折叠与展开
    toggleCollapse(){
        this.isCollapse = !this.isCollapse
    },
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath = activePath
    }
  }
}
</script>
<style lang="scss" scoped>
#app{
  height: 100%;
}

.home-container{
  height: 100%;
}
.el-header {
  background: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #dddddd;
  font-size: 20px;
  padding-left: 0;
  div{
    display: flex;
    justify-content: center;
    align-items: center;
    img{
      height: 40px;
      width: 50px;
    }
    span{
      margin-left: 15px;
    }
  }


}
.el-aside {
  background-color: #333744;
}

.iconfont{
  margin-right: 10px;
}
.el-menu{
  border: 0;
}
.toggle-button{
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #ffffff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
